/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cxhd.dc.controller;

import com.cxhd.dc.DataConfig;
import com.cxhd.dc.task.StartTask;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Administrator
 */
public class ConfigController extends Controller {

    public void index() {
        render("/config/dataConfig.jsp");
    }

    public void checkLogin() {
        String password = getPara("pw");

        if (DataConfig.getInstance().getLoginPW().equals(password)) {
            renderText("true");
        } else {
            renderText("false");
        }

    }

    public void configPage() {
        setAttr("key", "true");
        redirect("/dataConfig.jsp");
    }

    public void saveConfig() {
        Map<String, String[]> map = getParaMap();
        try {
            for (String key : map.keySet()) {

                Db.use("dcmris_en").update("replace into dataconfig set name='" + key + "',value='" + getPara(key) + "',other1='"+DataConfig.getInstance().getLocalhostSN()+"'");
            }
            DataConfig.getInstance().getConfigForDatabase().checkFolder();
            System.out.println(DataConfig.getInstance().getCenterHost());
            renderText("true");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("false");
        }
    }

    public void getConfig() {
        List<Record> list = Db.use("dcmris_en").find("select * from dataconfig where other1='"+DataConfig.getInstance().getLocalhostSN()+"'");
        Map<String, String> map = new HashMap<String, String>();
        if (!list.isEmpty()) {
            for (Record re : list) {
                map.put(re.getStr("name"), re.getStr("value"));
            }
        }
        renderJson(map);
    }

    public void startTask() {
        StartTask.getIntance().start();
        renderText("true");
    }

    public void closeTask() {
        StartTask.getIntance().stop();
        renderText("true");
    }

    public void restartTask() {
        StartTask.getIntance().stop();
        StartTask.getIntance().start();
        renderText("true");
    }

}
